/**
 * 通用CSV字段枚举分析工具 - 简洁版
 * 支持任意结构的CSV文件
 */

const fs = require('fs');
const Papa = require('papaparse');
const path = require('path');

/**
 * 判断字段是否具有唯一性（需要跳过）
 */
function isUniqueField(columnName, uniqueCount, totalCount) {
    // 方法1: 根据字段名判断
    const lowerName = columnName.toLowerCase();
    const idPatterns = ['_id', 'id', '编号', '序号', 'uuid', 'guid', 'code'];
    const nameMatch = idPatterns.some(pattern => {
        if (pattern === '_id') return lowerName === '_id';
        return lowerName.includes(pattern) && 
               (lowerName.endsWith('id') || lowerName.startsWith('id') || lowerName.includes('编号'));
    });
    
    // 方法2: 根据唯一值比例判断（唯一值超过50%认为是唯一性字段）
    const uniqueRatio = uniqueCount / totalCount;
    const ratioMatch = uniqueRatio > 0.5;
    
    return nameMatch || ratioMatch;
}

/**
 * 分析CSV文件
 */
function analyzeCSV(filePath) {
    return new Promise((resolve, reject) => {
        console.log(`\n分析文件: ${filePath}`);
        console.log('='.repeat(70));

        const fieldStats = {};
        const relationships = [];
        let totalRows = 0;
        let columns = [];
        
        const stream = fs.createReadStream(filePath, { encoding: 'utf-8' });

        Papa.parse(stream, {
            header: true,
            skipEmptyLines: true,
            
            step: function(result) {
                totalRows++;

                // 初始化
                if (totalRows === 1 && result.meta.fields) {
                    columns = result.meta.fields;
                    columns.forEach(col => {
                        fieldStats[col] = {
                            values: new Map(),
                            total: 0,
                            empty: 0
                        };
                    });
                }

                // 收集数据
                columns.forEach(col => {
                    const value = result.data[col];
                    if (!fieldStats[col]) return;
                    
                    fieldStats[col].total++;
                    
                    if (value === null || value === undefined || value === '' || value === '数据不存在') {
                        fieldStats[col].empty++;
                    } else {
                        const count = fieldStats[col].values.get(value) || 0;
                        fieldStats[col].values.set(value, count + 1);
                    }
                });

                if (totalRows % 100000 === 0) {
                    process.stdout.write(`\r已分析: ${totalRows.toLocaleString()} 行`);
                }
            },

            complete: function() {
                console.log(`\n✅ 完成！共 ${totalRows.toLocaleString()} 行\n`);
                
                // 确定哪些字段需要分析
                const analyzableFields = [];
                const skippedFields = [];
                
                columns.forEach(col => {
                    const stats = fieldStats[col];
                    const uniqueCount = stats.values.size;
                    
                    if (isUniqueField(col, uniqueCount, totalRows)) {
                        skippedFields.push({
                            name: col,
                            reason: `唯一值过多 (${uniqueCount.toLocaleString()}/${totalRows.toLocaleString()})`
                        });
                        console.log(`⏭️  跳过: ${col} - ${uniqueCount.toLocaleString()}个唯一值`);
                    } else {
                        analyzableFields.push(col);
                        console.log(`✅ 分析: ${col} - ${uniqueCount}个唯一值`);
                    }
                });
                
                console.log('');
                
                // 分析所有可能的字段关系
                if (analyzableFields.length >= 2) {
                    console.log('分析字段关系...\n');
                    
                    for (let i = 0; i < analyzableFields.length - 1; i++) {
                        for (let j = i + 1; j < analyzableFields.length; j++) {
                            const parentCol = analyzableFields[i];
                            const childCol = analyzableFields[j];
                            
                            const relation = analyzeRelationship(
                                parentCol, childCol, 
                                fieldStats[parentCol], fieldStats[childCol],
                                filePath
                            );
                            
                            if (relation) {
                                relationships.push(relation);
                            }
                        }
                    }
                }
                
                // 整理结果
                const results = {
                    filePath,
                    fileName: path.basename(filePath),
                    totalRows,
                    allColumns: columns,
                    analyzableFields,
                    skippedFields,
                    fieldStats: {},
                    relationships
                };
                
                // 只保留可分析字段的统计
                analyzableFields.forEach(col => {
                    const stats = fieldStats[col];
                    const sortedValues = Array.from(stats.values.entries())
                        .sort((a, b) => b[1] - a[1]);
                    
                    results.fieldStats[col] = {
                        uniqueCount: stats.values.size,
                        total: stats.total,
                        empty: stats.empty,
                        values: sortedValues
                    };
                });
                
                resolve(results);
            },

            error: reject
        });
    });
}

/**
 * 分析两个字段之间的关系
 */
function analyzeRelationship(parentCol, childCol, parentStats, childStats, filePath) {
    // 重新扫描文件建立关系
    return new Promise((resolve) => {
        const relationMap = new Map();
        let rowCount = 0;
        
        const stream = fs.createReadStream(filePath, { encoding: 'utf-8' });
        
        Papa.parse(stream, {
            header: true,
            skipEmptyLines: true,
            step: function(result) {
                rowCount++;
                const parentValue = result.data[parentCol];
                const childValue = result.data[childCol];
                
                if (parentValue && childValue && 
                    parentValue !== '数据不存在' && 
                    childValue !== '数据不存在') {
                    
                    if (!relationMap.has(parentValue)) {
                        relationMap.set(parentValue, new Set());
                    }
                    relationMap.get(parentValue).add(childValue);
                }
            },
            complete: function() {
                // 计算关系的有效性
                const parentUniqueCount = relationMap.size;
                let totalChildVariety = 0;
                
                relationMap.forEach(childSet => {
                    totalChildVariety += childSet.size;
                });
                
                const avgChildPerParent = totalChildVariety / parentUniqueCount;
                
                // 只保留有意义的关系（平均每个父值至少有1.5个不同的子值）
                if (avgChildPerParent >= 1.5) {
                    const mappings = {};
                    relationMap.forEach((childSet, parentValue) => {
                        mappings[parentValue] = Array.from(childSet).sort();
                    });
                    
                    resolve({
                        parent: parentCol,
                        child: childCol,
                        parentUniqueCount,
                        avgChildPerParent: avgChildPerParent.toFixed(2),
                        mappings
                    });
                } else {
                    resolve(null);
                }
            }
        });
    });
}

/**
 * 生成简洁的HTML报告
 */
function generateSimpleHtmlReport(results, outputPath) {
    const { fileName, totalRows, analyzableFields, skippedFields, fieldStats, relationships } = results;
    
    // 生成字段枚举表格
    let fieldTablesHtml = '';
    analyzableFields.forEach(col => {
        const stats = fieldStats[col];
        fieldTablesHtml += `
            <div class="field-section">
                <h3>${col}</h3>
                <div class="field-meta">
                    <span>枚举类型数: <strong>${stats.uniqueCount}</strong></span>
                    <span>总记录: ${stats.total.toLocaleString()}</span>
                    <span>空值: ${stats.empty.toLocaleString()}</span>
                    <span>覆盖率: ${((stats.total - stats.empty) / stats.total * 100).toFixed(1)}%</span>
                </div>
                <table>
                    <thead>
                        <tr>
                            <th width="50">#</th>
                            <th>枚举值</th>
                            <th width="120">出现次数</th>
                            <th width="80">占比</th>
                        </tr>
                    </thead>
                    <tbody>
                        ${stats.values.slice(0, 50).map(([value, count], idx) => {
                            const percent = (count / stats.total * 100).toFixed(2);
                            return `
                                <tr>
                                    <td>${idx + 1}</td>
                                    <td>${value}</td>
                                    <td>${count.toLocaleString()}</td>
                                    <td>${percent}%</td>
                                </tr>
                            `;
                        }).join('')}
                        ${stats.values.length > 50 ? `
                            <tr class="more-row">
                                <td colspan="4">... 还有 ${stats.values.length - 50} 个枚举值（已省略）</td>
                            </tr>
                        ` : ''}
                    </tbody>
                </table>
            </div>
        `;
    });
    
    // 生成关系分析
    let relationshipsHtml = '';
    if (relationships.length > 0) {
        relationships.forEach(rel => {
            const sortedMappings = Object.entries(rel.mappings)
                .sort((a, b) => b[1].length - a[1].length);
            
            relationshipsHtml += `
                <div class="relation-section">
                    <h3>${rel.parent} → ${rel.child}</h3>
                    <div class="relation-meta">
                        <span>${rel.parent}有 <strong>${rel.parentUniqueCount}</strong> 种</span>
                        <span>平均每个${rel.parent}包含 <strong>${rel.avgChildPerParent}</strong> 个${rel.child}</span>
                    </div>
                    <div class="relation-grid">
                        ${sortedMappings.map(([parentValue, childValues]) => `
                            <div class="relation-item">
                                <div class="parent-label">${parentValue} <span class="count">(${childValues.length})</span></div>
                                <div class="child-list">
                                    ${childValues.map(v => `<span class="child-tag">${v}</span>`).join('')}
                                </div>
                            </div>
                        `).join('')}
                    </div>
                </div>
            `;
        });
    } else {
        relationshipsHtml = '<p class="no-data">未发现有意义的字段层级关系</p>';
    }
    
    const html = `<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>字段枚举分析 - ${fileName}</title>
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body {
            font-family: "Microsoft YaHei", Arial, sans-serif;
            background: #f5f5f5;
            padding: 20px;
            line-height: 1.6;
        }
        .container {
            max-width: 1400px;
            margin: 0 auto;
            background: white;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        .header {
            background: #2c3e50;
            color: white;
            padding: 25px 30px;
            border-radius: 8px 8px 0 0;
        }
        .header h1 {
            font-size: 1.8em;
            margin-bottom: 8px;
        }
        .header .subtitle {
            opacity: 0.9;
        }
        .summary {
            display: flex;
            gap: 15px;
            padding: 20px 30px;
            background: #ecf0f1;
            border-bottom: 2px solid #ddd;
        }
        .summary-item {
            flex: 1;
            text-align: center;
            padding: 10px;
        }
        .summary-item .label {
            font-size: 0.9em;
            color: #666;
            margin-bottom: 5px;
        }
        .summary-item .value {
            font-size: 1.8em;
            font-weight: bold;
            color: #2c3e50;
        }
        .content {
            padding: 30px;
        }
        h2 {
            color: #2c3e50;
            margin: 30px 0 20px 0;
            padding-bottom: 10px;
            border-bottom: 2px solid #3498db;
            font-size: 1.5em;
        }
        .skipped-list {
            background: #fff3cd;
            padding: 15px 20px;
            border-radius: 5px;
            margin-bottom: 30px;
            border-left: 4px solid #ffc107;
        }
        .skipped-list ul {
            list-style: none;
            padding-left: 0;
        }
        .skipped-list li {
            padding: 5px 0;
            color: #856404;
        }
        .field-section {
            margin-bottom: 40px;
        }
        .field-section h3 {
            color: #3498db;
            font-size: 1.3em;
            margin-bottom: 10px;
        }
        .field-meta {
            display: flex;
            gap: 20px;
            margin-bottom: 15px;
            padding: 10px;
            background: #f8f9fa;
            border-radius: 5px;
        }
        .field-meta span {
            color: #666;
        }
        .field-meta strong {
            color: #2c3e50;
            font-size: 1.1em;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 20px;
        }
        th {
            background: #34495e;
            color: white;
            padding: 12px;
            text-align: left;
            font-weight: 600;
        }
        td {
            padding: 10px 12px;
            border-bottom: 1px solid #ddd;
        }
        tr:nth-child(even) {
            background: #f8f9fa;
        }
        tr:hover {
            background: #e9ecef;
        }
        .more-row td {
            text-align: center;
            font-style: italic;
            color: #666;
        }
        .relation-section {
            margin-bottom: 40px;
        }
        .relation-section h3 {
            color: #27ae60;
            font-size: 1.3em;
            margin-bottom: 10px;
        }
        .relation-meta {
            display: flex;
            gap: 30px;
            margin-bottom: 15px;
            padding: 10px;
            background: #f8f9fa;
            border-radius: 5px;
        }
        .relation-meta span {
            color: #666;
        }
        .relation-meta strong {
            color: #27ae60;
        }
        .relation-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(400px, 1fr));
            gap: 15px;
        }
        .relation-item {
            border: 1px solid #ddd;
            border-radius: 5px;
            padding: 15px;
            background: white;
        }
        .parent-label {
            font-weight: bold;
            color: #2c3e50;
            margin-bottom: 10px;
            padding-bottom: 8px;
            border-bottom: 2px solid #3498db;
        }
        .parent-label .count {
            float: right;
            background: #3498db;
            color: white;
            padding: 2px 10px;
            border-radius: 12px;
            font-size: 0.9em;
        }
        .child-list {
            display: flex;
            flex-wrap: wrap;
            gap: 8px;
        }
        .child-tag {
            background: #ecf0f1;
            padding: 5px 10px;
            border-radius: 4px;
            font-size: 0.9em;
            color: #2c3e50;
        }
        .no-data {
            text-align: center;
            color: #999;
            padding: 30px;
            font-style: italic;
        }
        .footer {
            text-align: center;
            padding: 20px;
            color: #666;
            border-top: 1px solid #ddd;
            margin-top: 30px;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>字段枚举分析报告</h1>
            <div class="subtitle">${fileName} · ${new Date().toLocaleString('zh-CN')}</div>
        </div>

        <div class="summary">
            <div class="summary-item">
                <div class="label">总行数</div>
                <div class="value">${totalRows.toLocaleString()}</div>
            </div>
            <div class="summary-item">
                <div class="label">总字段数</div>
                <div class="value">${results.allColumns.length}</div>
            </div>
            <div class="summary-item">
                <div class="label">分析字段</div>
                <div class="value">${analyzableFields.length}</div>
            </div>
            <div class="summary-item">
                <div class="label">跳过字段</div>
                <div class="value">${skippedFields.length}</div>
            </div>
            <div class="summary-item">
                <div class="label">字段关系</div>
                <div class="value">${relationships.length}</div>
            </div>
        </div>

        <div class="content">
            ${skippedFields.length > 0 ? `
                <div class="skipped-list">
                    <strong>⏭️ 跳过的字段（具有唯一性）：</strong>
                    <ul>
                        ${skippedFields.map(f => `<li>• ${f.name} - ${f.reason}</li>`).join('')}
                    </ul>
                </div>
            ` : ''}

            <h2>📊 字段关系分析</h2>
            ${relationshipsHtml}

            <h2>📋 字段枚举详情</h2>
            ${fieldTablesHtml}
        </div>

        <div class="footer">
            CSV字段枚举分析工具 · 支持任意结构的CSV文件
        </div>
    </div>
</body>
</html>`;

    fs.writeFileSync(outputPath, html, 'utf-8');
}

/**
 * 主函数
 */
async function main() {
    const outputDir = 'output';
    
    // 扫描output目录下的所有CSV文件
    const csvFiles = [];
    try {
        const files = fs.readdirSync(outputDir);
        files.forEach(file => {
            if (file.endsWith('.csv')) {
                const filePath = `${outputDir}/${file}`;
                const stats = fs.statSync(filePath);
                csvFiles.push({
                    path: filePath,
                    name: file,
                    size: (stats.size / (1024 * 1024)).toFixed(2)
                });
            }
        });
    } catch (e) {
        console.error('读取output目录失败:', e);
        return;
    }

    if (csvFiles.length === 0) {
        console.log('未在output目录找到CSV文件');
        return;
    }

    console.log('='.repeat(70));
    console.log('CSV字段枚举分析工具（通用版）');
    console.log('='.repeat(70));
    console.log('\n找到以下CSV文件:');
    csvFiles.forEach((file, idx) => {
        console.log(`${idx + 1}. ${file.name} (${file.size} MB)`);
    });

    // 分析每个文件
    for (let i = 0; i < csvFiles.length; i++) {
        const file = csvFiles[i];
        console.log(`\n${'='.repeat(70)}`);
        console.log(`正在分析 [${i + 1}/${csvFiles.length}]: ${file.name}`);
        console.log('='.repeat(70));

        try {
            const results = await analyzeCSV(file.path);
            
            // 生成报告
            const outputFileName = file.name.replace('.csv', '_枚举分析.html');
            const outputPath = `${outputDir}/${outputFileName}`;
            
            console.log('\n生成HTML报告...');
            generateSimpleHtmlReport(results, outputPath);
            
            console.log(`✅ 报告已生成: ${outputPath}\n`);
            
        } catch (error) {
            console.error(`❌ 分析失败:`, error);
        }
    }

    console.log('='.repeat(70));
    console.log('所有文件分析完成！');
    console.log('='.repeat(70));
}

if (require.main === module) {
    main();
}

module.exports = { analyzeCSV, generateSimpleHtmlReport };

